import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/login/login.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/home.vue'),
      children: [
        {
          path: '/home/renwu',
          name: '',
          component: () => import('../views/home/child/renwu.vue'),
        },
        {
          path: '/home/detail/:id',
          name: '',
          component: () => import('../views/home/child/detail.vue'),
        },
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  if (!token && to.name !== 'login') {
    next({ path: "/" })
  } else {
    next()
  }
})

export default router
